package com.ym.soso.service.search.service;

import com.ym.base.domain.MobileSearchParam;
import com.ym.common.utils.StringUtil;
import com.ym.core.es.query.QueryParser;
import com.ym.core.es.service.AbstractHeightWordOperationService;
import com.ym.soso.domain.entity.PolyclinicEntity;
import com.ym.soso.enums.PublishStatusEnum;
import com.ym.soso.service.IPolyclinicSV;
import com.ym.soso.service.search.index.PolyclinicIndex;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 综合门诊
 *
 * @author: 李涛
 * @version: 2020年01月07日 14:17
 */
@Service
@Slf4j
public class PolyclinicIndexService extends AbstractHeightWordOperationService<PolyclinicIndex, PolyclinicEntity> {

	@Autowired
	private IPolyclinicSV polyclinicSV;

	@Override
	public List<PolyclinicEntity> getData() {
		List<PolyclinicEntity> initData = polyclinicSV.selectAll();
		return initData;
	}

	/**
	 * 数据库实体转换为luce实体
	 *
	 * @param data
	 * @return
	 */
	@Override
	public PolyclinicIndex beanCovert(PolyclinicEntity data) {
		PolyclinicIndex build = new PolyclinicIndex();
		build.setTheName(data.getTheName());
		build.setId(data.getId().toString());
		build.setPublishStatus(data.getPublishStatus());
		build.setProv(data.getProv());
		build.setCity(data.getCity());
		build.setArea(data.getArea());
		return build;
	}


	@Override
	protected QueryParser buildSearchIdsConditions(MobileSearchParam args) {
		String prov = args.getProv();
		String city = args.getCity();
		String area = args.getArea();
		String keyword = args.getKeyword();
		// 构建查询语句
		QueryParser queryParser = new QueryParser(dataTypeClass);
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
		if (StringUtil.isNotBlank(prov)) {
			boolQueryBuilder.filter(QueryBuilders.termQuery("prov", prov));
		}
		if (StringUtil.isNotBlank(city)) {
			boolQueryBuilder.filter(QueryBuilders.termQuery("city", city));
		}
		if (StringUtil.isNotBlank(area)) {
			boolQueryBuilder.filter(QueryBuilders.termQuery("area", area));
		}
		boolQueryBuilder.filter(QueryBuilders.termQuery("publishStatus", PublishStatusEnum.HAS_BEEN_RELEASED.getKey()));
		MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, "tags", "serviceItem", "theName");
		multiMatchQueryBuilder.minimumShouldMatch("25%");
		boolQueryBuilder.must(multiMatchQueryBuilder);
		queryParser.query(boolQueryBuilder, new String[]{"id"});
		return queryParser;
	}
}
